package com.naihe.anderson.sms.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.adapter.MoneySetAdapter;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.manager.RecordMoneyMgr;
import com.naihe.anderson.sms.util.PxDipChangeUtil;
import com.naihe.anderson.sms.widgets.RoundedRectListView;

public class MoneySetActivity extends Activity implements OnClickListener, OnItemClickListener{

	NhSmsApp nhSmsApp;
	RecordMoneyMgr moneyMgr;
	
	Button btn_back;
	int px = 0;
	
	public static final int MSG_WHAT_DATECHANGE_ACTIVITY = 0;
	Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_WHAT_DATECHANGE_ACTIVITY:
				break;
			default:
				break;
			}
		}
	};
	
	private LayoutParams changeLayoutParams(int size, LinearLayout layout){
		LayoutParams para = (LayoutParams) layout.getLayoutParams();
		para.height = size * px;
		para.width = LayoutParams.FILL_PARENT;
		return para;
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.money_set_layout);
        nhSmsApp = (NhSmsApp) getApplication();
        nhSmsApp.setActivity(this);
        moneyMgr = nhSmsApp.getRecordMoneyMgr();
        px = PxDipChangeUtil.pxToDip(44, MoneySetActivity.this);
        
        initView();
        
        initPreferenceList();
        initSetList();
    }
	
	private void initView(){
		btn_back = (Button) findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
	}
	
	private void toFinishActivity(){
		nhSmsApp.setActivity(null);
		MoneySetActivity.this.finish();
		System.gc();
	}
	
	private void initPreferenceList(){
		LinearLayout preferenceLayout = (LinearLayout) findViewById(R.id.preferenceLayout);
		
		List<String> objects = new ArrayList<String>();
		objects.add("");
		RoundedRectListView preferenceList = (RoundedRectListView) findViewById(R.id.preferenceList);
		MoneySetAdapter preferenceAdapter = new MoneySetAdapter(MoneySetActivity.this, objects);
		preferenceList.setAdapter(preferenceAdapter);
		preferenceList.setOnItemClickListener(this);
		preferenceLayout.setLayoutParams(changeLayoutParams(objects.size(), preferenceLayout));
	}
	
	private void initSetList(){
		LinearLayout firstLayout = (LinearLayout) findViewById(R.id.firstLayout);
		
		List<String> objects = new ArrayList<String>();
		objects.add("");
		objects.add("");
		objects.add("");
		RoundedRectListView mediaSetList = (RoundedRectListView) findViewById(R.id.mediaSetList);
		MoneySetAdapter mediaSetAdapter = new MoneySetAdapter(MoneySetActivity.this, objects);
		mediaSetList.setAdapter(mediaSetAdapter);
		mediaSetList.setOnItemClickListener(this);
		firstLayout.setLayoutParams(changeLayoutParams(objects.size(), firstLayout));
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.back_btn){
			toFinishActivity();
		} else {
			
		}
	}
    
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 截获按键事件
        if(KeyEvent.KEYCODE_BACK == keyCode){
        	toFinishActivity();
        	return true;
		}

        return super.onKeyDown(keyCode, event);
    }
}
